﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace websitev2.Controllers
{
    public class InfoController : Controller
    {
  
        public Models.StartStopEntities db = new Models.StartStopEntities(); 


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult WindowsApplications(string ID)
        {

            int _appTypeID = 0;
    


            var type = db.WindowAppTypes.FirstOrDefault(a => a.TypeName == ID.Replace("-"," "));
            
            // The item has not been found by the typename. 
            if (type == null)
            {
                // Maybe it's passed an interger. 
                try
                {
                    _appTypeID = Int32.Parse(ID);
                    var AppType = db.WindowAppTypes.FirstOrDefault(a => a.TypeID == _appTypeID); 
                            
                    if (AppType != null)
                    {
                        ViewBag.PageTitle = AppType.TypeName + " Applications"; 
                    }
                }
                catch { }
            }
            else
            {
                // It has been found
                _appTypeID = type.TypeID;
                ViewBag.PageTitle = type.TypeName + " Applications";   
            }

            if (_appTypeID == 0)
            {
                    // This is the drop through where the app type has not been found. 
                    _appTypeID = 1;
                    ViewBag.PageTitle = "Browser Applications";
                
            }

            // Check to see if all was sent. 
            if (ID.ToLower() == "all")
            {
                var items = db.WindowAppLists
                     .Where(a => a.Description != null && a.Description!="")
                     .OrderBy(a=> a.Description) 
                   .ToList();
                ViewBag.PageTitle = "Showing all known Windows Applications"; 
                return View(items); 
            }
            else
            {
               var items = db.WindowAppLists
                   .Where(a => a.TypeID == _appTypeID && a.Description!=null && a.Description!="")
                   .OrderBy(a=> a.Description) 
                   .ToList();
               return View(items); 
            }

        }

    }
}
